package com.free4inno.pagesite.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.free4inno.pagesite.utils.HttpUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;


@Slf4j
@Service
public class ResourceService {
    @Autowired
    private HttpUtils httpUtils;

    @Value("${zhi.openapi.address}")
    private String zhiAddress;

    @Value("${zhi.openapi.appkey}")
    private String zhiAppkey;


    /**
     * 搜索数据集资源
     * （根据请求参数拼接url，调用utils层http工具调用openapi，结果直接返回）
     */
    public String searchPageSite(String queryStr, Integer pageNum, Integer pageSize, JSONArray labelNames) {

        log.info("searchPageSite ---- start ----");
//        labelNames.add("网络书签");
        String url = zhiAddress + "search?"
                + "appkey=" + zhiAppkey + "&"
                + "pageNum=" + pageNum + "&"
                + "pageSize=" + pageSize + "&"
                + "queryStr=" + queryStr + "&"
                + "groupNames=" + "[]" + "&"
                + "labelNames=" + JSON.toJSONString(labelNames);
        log.info("searchPageSite -- " + url);
        try {
            String jsonResponse = httpUtils.getRequest(url);
            log.info("searchPageSite ---- end ----");
            return jsonResponse;
        } catch (Exception e) {
            e.printStackTrace();
            log.error("searchPageSite ---- error ----");
            return "";
        }
    }

    /**
     * 获取资源详情
     * （根据请求参数拼接url，调用utils层http工具调用openapi，结果直接返回）
     */
    public String getResourceDetail(Integer id) {
        log.info("getResourceDetail ---- start ---- id:" + id);
        String url = zhiAddress + "detail?"
                + "appkey=" + zhiAppkey + "&"
                + "id=" + id;
        log.info("getResourceDetail -- " + url);
        try {
            String jsonResponse = httpUtils.getRequest(url);
            log.info("getResourceDetail ---- end ----");
            return jsonResponse;
        } catch (Exception e) {
            e.printStackTrace();
            log.error("getResourceDetail ---- error ----");
            return "";
        }
    }
}
